package com.bruce.springbootDemo.web.restFul;

/**
 * Created by bruce on 18/11/22 -- 14:51
 */
public class ResponseTool {
    private static ResponseEntity responseEntity = new ResponseEntity();

    private static ResponseEntity getInstance() {
        if (responseEntity == null) {
            responseEntity = new ResponseEntity();
        }
        return responseEntity;
    }

    public static ResponseEntity ok() {
        return ok(null);
    }

    public static ResponseEntity ok(Object data) {
        ResponseEntity object = getInstance();
        object.setCode(ResponseStatus.ok.getCode());
        object.setMsg(ResponseStatus.ok.getMsg());
        object.setData(data);
        return object;
    }

    public static ResponseEntity fail(ResponseStatus status) {
        ResponseEntity object = getInstance();
        object.setCode(status.getCode());
        object.setMsg(status.getMsg());
        object.setData(null);
        return object;
    }

    public static ResponseEntity fail(Integer code, String msg) {
        ResponseEntity object = getInstance();
        object.setCode(code);
        object.setMsg(msg);
        object.setData(null);
        return object;
    }
}
